package com.caifu.controller.healthArchive;

import com.caifu.bean.HealthyArchiveSearchBean;
import com.caifu.controller.AbstractController;
import com.caifu.enums.LabelTypeEnum;
import com.caifu.pojo.*;
import com.caifu.service.ElderlyInfoService;
import com.caifu.service.IBuildingService;
import com.caifu.service.IHealthArchiveService;
import com.caifu.util.Constance;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/healthArchive/elderly")
public class ElderLyController extends AbstractController {

    @Autowired
    private IHealthArchiveService healthArchiveService;
    @Autowired
    private IBuildingService buildingService;
    @Autowired
    private ElderlyInfoService elderlyInfoService;

    /**
    * 
    * 功能描述：老人信息列表
    *
    * @Author  GZ
    * @Date   2020/11/11 14:02
    */
    @RequestMapping("oldManList")
    public String oldManList(Model model){
        SysUser sysUser =getUser();
        try {
            //查询养老机构楼苑信息列表
            List<YlBuilding> buildingList = buildingService
                    .queryBuildingInfo(sysUser.getCurrentPensionNo());
            model.addAttribute("buildingList",buildingList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "healthArchive/oldManList";
    }
    /**
    * 
    * 功能描述：获取老人信息数据
    *
    * @Author  GZ
    * @Date   2020/11/11 14:04
    */
    @RequestMapping("pagedQueryOldManData")
    @ResponseBody
    public JSONObject pagedQueryOldManData(int pageNum
            , int pageSize,String buildingNo,String floorNo
            ,String roomName,String bedName,String userName,String archiveFlag){
        try {
            SysUser user =getUser();
            HealthyArchiveSearchBean searchBean = new HealthyArchiveSearchBean();
            searchBean.setArchiveFlag(archiveFlag);
            searchBean.setBedName(bedName);
            searchBean.setBuildingNo(buildingNo);
            searchBean.setFloorNo(floorNo);
            searchBean.setRoomName(roomName);
            searchBean.setUserName(userName);
            return healthArchiveService.pagedQueryOldManInfoData(pageNum
                    ,pageSize,user,searchBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
    * 
    * 功能描述：创建健康档案
    *
    * @Author  GZ
    * @Date   2020/11/12 14:30
    */
    @RequestMapping("addBaseHealthy")
    public String addBaseHealthy(String userNo,Model model){
        try {
            JSONObject userInfo = healthArchiveService.queryElderlyUserInfo(userNo);
            model.addAttribute("userInfo",userInfo);
            model.addAttribute("userNo",userNo);
            model.addAttribute("nations",
                    elderlyInfoService.getYsLabelListByParentCode(LabelTypeEnum.MZ.getKey()));
            model.addAttribute("educations",
                    elderlyInfoService.getYsLabelListByParentCode(LabelTypeEnum.WHCD.getKey()));
            model.addAttribute("marriages",
                    elderlyInfoService.getYsLabelListByParentCode(LabelTypeEnum.HYQK.getKey()));
            //查询健康档案信息
            YlHealthyArchives archives = healthArchiveService.queryHealthyArchivesByUserNo(userNo);
            if(null==archives){
                archives = new YlHealthyArchives();
                archives.setInNo(Constance.disponseDate(Constance
                        .Null2EmptyTrim(userInfo.getString("inDate"))));
                archives.setNation(userInfo.get("nation").toString());
                archives.setAddress(userInfo.get("userAddress").toString());
                archives.setSex(userInfo.get("sex").toString());
                archives.setWorkPlace(userInfo.get("workPlace").toString());
                archives.setJob(userInfo.get("occupation").toString());
                archives.setInDate(userInfo.get("inDate").toString());
            }
            model.addAttribute("archives",archives);
            //查询个人史
            YlUserHistory userHistory = healthArchiveService.queryUserHistoryInfo(userNo);
            if(null==userHistory){
                userHistory = new YlUserHistory();
                userHistory.setEducation(userInfo.get("eductionCode").toString());
            }
            model.addAttribute("userHistory", userHistory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "healthArchive/addBasicHealthyInfo";
    }
    /**
    * 
    * 功能描述：保存健康档案信息
    *
    * @Author  GZ
    * @Date   2020/11/12 14:34
    */
    @RequestMapping("saveBaseHealthyInfo")
    @ResponseBody
    public JSONObject saveBaseHealthyInfo(YlHealthyArchives healthyArchives
            , YlUserHistory history){
        SysUser sysUser =getUser();
        try {
            return healthArchiveService.saveBasicHealthyInfo(healthyArchives
                    ,history,sysUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * 功能描述：创建体格数据信息
     *
     * @Author  GZ
     * @Date   2020/11/12 14:30
     */
    @RequestMapping("addPhysiqueCheckInfo")
    public String addPhysiqueCheckInfo(String userNo,Model model){
        try {
            JSONObject userInfo = healthArchiveService.queryElderlyUserInfo(userNo);
            model.addAttribute("userInfo",userInfo);
            //查询体格价检查信息
            YlPhysiqueCheck checkInfo = healthArchiveService.queryUserPhysiqueCheckByUserNo(userNo);
            model.addAttribute("checkInfo",checkInfo);
            model.addAttribute("userNo",userNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "healthArchive/addPhysiqueCheckInfo";
    }

    /**
     *
     * 功能描述：保存体格检查记录
     *
     * @Author  GZ
     * @Date   2020/11/12 14:34
     */
    @RequestMapping("savePhysiqueCheckInfo")
    @ResponseBody
    public JSONObject savePhysiqueCheckInfo(YlPhysiqueCheck physiqueCheck){
        SysUser user =getUser();
        try {
            return healthArchiveService.savePhysiqueCheckInfo(physiqueCheck,user);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
    * 
    * 功能描述：健康档案详情
    *
    * @Author  GZ
    * @Date   2020/11/16 9:57
    */
    @RequestMapping("healthArchiveDetail")
    public String healthArchiveDetail(String userNo,Model model){
        try {
            //查询老人信息
            JSONObject userInfo = healthArchiveService.queryElderlyUserInfo(userNo);
            model.addAttribute("userInfo",userInfo);
            model.addAttribute("userNo",userNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "healthArchive/basicHealthInfoDetail";
    }
    /**
    * 
    * 功能描述：查询健康档案基本信息
    *
    * @Author  GZ
    * @Date   2020/11/16 10:14
    */
    @RequestMapping("obtainOldManBasicInfo")
    @ResponseBody
    public JSONObject obtainOldManBasicInfo(String userNo){
        JSONObject result = new JSONObject();
        try {
            //查询基本信息
            YlHealthyArchives archives = healthArchiveService
                    .queryHealthyArchivesByUserNo(userNo);
            //查询个人史
            YlUserHistory history=healthArchiveService
                    .queryUserHistoryInfo(userNo);
            if(null==history){
                history = new YlUserHistory();
            }
            //查询体格检查
            YlPhysiqueCheck checkInfo = healthArchiveService
                    .queryUserPhysiqueCheckByUserNo(userNo);
            if(null==checkInfo){
                checkInfo = new YlPhysiqueCheck();
            }
            result.put("archives",archives);
            result.put("history",history);
            result.put("checkInfo",checkInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
